using System;
using System.Data;
using System.Linq;
using System.Text;

namespace PpSmsSender.Db
{

    #region Entities
    public partial class SmsMessage
    {
        #region Statuses enum
        public enum Statuses
        {
            Unknown = 0,
            Sent = -1,
            SendDone = -2,
            SendException = -5,
            SendFailed = -9
        }
        #endregion

        #region Types enum
        public enum Types
        {
            Unknown = 0,
            ToSend = 1,
            Sent = 2,
            Recieved = 3,
            FailedSend = 4,
            Queued = 5,
            ForwardedRecieved = 6,
            AckedSent = 7
        }
        #endregion

        public SmsMessage()
        {
            this.Id = Guid.NewGuid();
            this.Priority = 8;
            this.RecievedBy = "";
            this.Number = "";
        }

        public SmsMessage(string text, string number)
            : this()
        {
            this.Text = text;
            this.Number = number;
        }

        public Types Type
        {
            get { return (Types)this.Typ; }
            set { this.Typ = (int)value; }
        }

        public PhoneNumber Number
        {
            get { return this.Number_Number; }
            set { this.Number_Number = value; }
        }

        public PhoneNumber RecievedBy
        {
            get { return this.RecievedBy_Number; }
            set { this.RecievedBy_Number = value; }
        }

        public bool InsertOrAttach(SmsDataContext db)
        {
            if (db.SmsMessages.Any(e => e.Id == this.Id))
            {
                db.SmsMessages.Attach(this);
                db.ObjectStateManager.ChangeObjectState(this, EntityState.Modified);
                return true;
            }
            else
            {
                db.SmsMessages.AddObject(this);
                return false;
            }
        }


        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("text      :" + this.Text);
            sb.AppendLine("number    :" + this.Number);
            sb.AppendLine("recievedby:" + this.RecievedBy);
            sb.AppendLine("result    :" + this.Result);
            sb.AppendLine("status    :" + this.Status);
            sb.AppendLine("reference :" + this.Reference);
            sb.AppendLine("sentAt    :" + this.SentAt);
            sb.AppendLine("recivedAt :" + this.RecievedAt);
            return sb.ToString();
        }
    }
    #endregion
}